<template>
  <div>
    <el-row style="margin-bottom:20px;display:flex;justify-content:flex-start">
      <el-input
        placeholder="人员姓名"
        style="width:20%;margin-right:10px"
        v-model="postData.hash.username"
        clearable
      ></el-input>
      <el-input
        placeholder="专业技术"
        style="width:20%;margin-right:10px"
        v-model="postData.hash.tec_type"
        clearable
      ></el-input>
      <el-select
        v-model="postData.hash.qualifyname"
        placeholder="请选择资格名称"
        style="margin-right:10px"
        clearable
      >
        <el-option
          v-for="item in itemdata"
          :key="item.value"
          :label="item.hash.code_name"
          :value="item.hash.code_id"
        ></el-option>
      </el-select>

      <el-input
        placeholder="专业名称"
        style="width:20%;margin-right:10px"
        v-model="postData.hash.tec_name"
        clearable
      ></el-input>
      <el-date-picker
        v-model="postData.hash.grantime"
        style="margin-right:10px"
        type="daterange"
        rangeSeparator="至"
        startPlaceholder="开始日期"
        endPlaceholder="结束日期"
        valueFormat="yyyy-MM-dd"
      ></el-date-picker>

      <el-button size="mini" type="primary" @click="getpostlist">搜索</el-button>
    </el-row>

    <el-row>
      <el-button size="mini" @click="showaddpost">添加技术职称</el-button>
      <el-button size="mini" type="primary" @click="amandpost">编辑</el-button>
      <el-button size="mini" type="success" @click="deletepost">删除</el-button>
    </el-row>
    <el-divider></el-divider>
    <posttable
      :tableData="postlist"
      @itemvals="itemvals"
      :pagobj.sync="pagobj"
      :ischeck="false"
      id="tableExcel"
    ></posttable>

    <addpost :itemdata="itemdata" v-if="isaddpost" :itemvalname="itemvalname" :chousebtn="btnvalue"></addpost>
  </div>
</template>

<script>
import addpost from '~/components/person/workpost/addpost.vue';
import posttable from '~/components/person/workpost/posttable.vue';
import FileSaver from 'file-saver';
import XLSX from 'xlsx';

export default {
  data() {
    return {
      dialogVisible: false,
      ts: this.newStore(),
      postData: {
        cols: [],
        hash: {
          username: '',
          tec_type: '',
          tec_name: '',
          grantime: [],
          qualifyname: '',
          cernum: '',
          pid: '',
          id: '',
        },
      },
      pagesize: 0,
      currentPage: 0,
      pagobj: {
        // 分页
        currentPage3: 1,
        pagesize: 10,
        total: 10,
      },
      postidarr: [], // 删除所用的数组id
      isaddpost: false, // 添加弹窗
      btnvalue: 0,
      Object: '',
      itemval: {}, // 编辑传回来的d对象
      cnumber: '',
      postlist: [],
      itemdata: [], // 项目类型
      changelist: {},
    };
  },
  components: {
    addpost,
    posttable,
  },

  created() {
    this.getpostlist();
    this.getitem();
  },
  methods: {
    // 删除技术职称
    deletepost() {
      // console.log(this.postidarr, "0");
      if (this.postidarr.length > 1) {
        this.$confirm('是否批量删除技术职称信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          const req = {
            handle:
              '1CB43CC1631B5D5B6D910BDD88FB08CC450715EB6255AFE3480E19032604E39664EDEA66339D7026',
            id: this.postidarr,
          };
          this.spost(this.ts, '/mainservlet', req).then((ts) => {
            if (ts.d.sflag == 'success') {
              this.$message({
                type: 'success',
                message: '删除成功!',
              });
              this.getpostlist();
            }
          });
        });
      } else if (this.postidarr.length == 1) {
        this.$confirm('是否删除该技术职称信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          const req = {
            handle:
              '1CB43CC1631B5D5B6D910BDD88FB08CC450715EB6255AFE3480E19032604E39664EDEA66339D7026',
            id: this.postidarr,
          };
          this.spost(this.ts, '/mainservlet', req).then((ts) => {
            if (ts.d.sflag == 'success') {
              this.$message({
                type: 'success',
                message: '删除成功!',
              });
              this.getpostlist();
            }
          });
        });
      } else {
        this.$alert('请选择技术职称', '提示');
      }
    },
    // 编辑技术职称
    amandpost() {
      (this.dialogVisible = true), (this.btnvalue = 2);
      if (Object.keys(this.itemval).length == 1) {
        let itemvals = {};
        itemvals = this.itemval[0];
        this.itemvalname = itemvals;
        // console.log(this.itemvalname,'0123')
        this.reload('isaddpost');
      } else {
        this.$alert('编辑只能编辑一个', '提示');
      }
    },
    // table 传给父组件
    itemvals(val) {
      // console.log(val, "wwwwww");
      this.itemval = val;
      this.postidarr = [];
      for (let i = 0; i < this.itemval.length; i++) {
        this.postidarr.push(this.itemval[i].hash.id);
      }
      // console.log(this.postidarr, "23");
    },
    // 监听子组件的(添加)
    addpost(val) {
      this.Object = {};
      this.Object = val;
      // val.hash.holder = val.hash.holders;
      // console.log(val, "22223322");
      // this.carlist.unshift(val);
      // console.log(this.postlist, "66666");
    },
    // 公共组件刷新
    reload(partsname) {
      // console.log(partsname);
      this[partsname] = false;
      this.$nextTick(() => {
        this[partsname] = true;
      });
    },
    // 点击添加技术职称信息
    showaddpost() {
      (this.dialogVisible = true), (this.btnvalue = 1);
      this.reload('isaddpost');
    },
    // 分页
    handleCurrentChange() {
      this.getpostlist();
    },
    // 获取项目类型
    getitem() {
      const req = {
        handle: '67A5B6FEF17D97DECF116BDF95B95C0B167942DD3F1604F9',
        supid: '0202',
        codetype: '02',
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        if (ts.d.sflag == 'success') {
          this.itemdata = ts.d.data.operation1;
          // console.log(this.itemdata,'888')
        }
      });
    },
    // 获取列表
    getpostlist() {
      // console.log("2222");
      if (this.postData.hash.grantime == null) {
        this.postData.hash.grantime = [];
      }

      const req = {
        handle:
          '1CB43CC1631B5D5B6D910BDD88FB08CC450715EB6255AFE3AA93D3E1306FF758',
        username: this.postData.hash.username,
        tec_type: this.postData.hash.tec_type,
        tec_name: this.postData.hash.tec_name,
        grantime1: this.postData.hash.grantime[0],
        grantime2: this.postData.hash.grantime[1],
        id: this.postData.hash.id,
        cernum: this.postData.hash.cernum,
        qualifyname: this.postData.hash.qualifyname,
        start: (this.pagobj.currentPage3 - 1) * this.pagobj.pagesize,
        limit: this.pagobj.pagesize,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.postlist = ts.d.data.operation1;
        this.pagobj.total = parseInt(ts.d.data.operation2[0].hash.allrs);
        // console.log(this.postlist,'789')
      });
    }
  },
};
</script>

